using Project.Api.Entity;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Project.Api.Database;
using Microsoft.EntityFrameworkCore;
using System;

namespace Project.Api.Repository
{
    public class EfRepository<T> : IRespository<T> where T : BaseEntity
    {

        private ProjectDb _db=new ProjectDb();

        private DbSet<T> _entity;

        protected DbSet<T> Entity
        {
            get
            {
                if(_entity==null)
                {
                    _entity=_db.Set<T>();
                }
                return _entity;
            }
        }
        public IQueryable<T> Table
        {
            get
            {
                return Entity.AsQueryable<T>();
            }
        }

        public void Delete(object id)
        {
            var s = Entity.Where(x=>x.id==(int)id).FirstOrDefault();
            _db.Remove(s);
            _db.SaveChanges();
        }

        public void DeleteBulk(IEnumerable<object> ids)
        {
            var sl = new List<int>();
            foreach(var item in ids)
            {
                var tmp=(int)item;
                sl.Add(tmp);
            }
            var ss = Entity.Where(x=>sl.Contains(x.id)).ToList();
            _db.RemoveRange(ss);
            _db.SaveChanges();
        }

        public T GetById(object id)
        {
            return Entity.Where(x=>x.id==(int)id).FirstOrDefault();
        }

        public void Insert(T entity)
        {
            throw new System.NotImplementedException();
        }

        public Task InsertAsync(T entity)
        {
            throw new System.NotImplementedException();
        }

        public void InsertBulk(IEnumerable<T> entities)
        {
            throw new System.NotImplementedException();
        }

        public Task InsertBulkAsync(IEnumerable<T> entities)
        {
            throw new System.NotImplementedException();
        }

        public void Update(T entity)
        {
            throw new System.NotImplementedException();
        }

        public void UpdateBulk(IEnumerable<T> entities)
        {
            throw new System.NotImplementedException();
        }
    }

}